﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.Client.templates;
using ECare.BUS;
using ECare.BUS.Data;

namespace ECare.Client.frms
{
    public partial class frmPlanQuery : frmInfo
    {
        public frmPlanQuery()
        {
            InitializeComponent();


            this.Load += frmPlanQuery_Load;
        }

        void frmPlanQuery_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();

            var formulas = FormulaHelper.GetFormulaInfos();

            foreach (var formula in formulas)
            {
                this.cbFormulas.Items.Add(formula);
            }

            this.cbFormulas.Items.Insert(0,new FormulaInfo { FormulaId=-1,FormulaName="请选择(please select)"});

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string searchwhere = "1=1";

            if (this.cbIsEnabled.Checked)
            {
                if (this.dateEnd.Value < this.dateStart.Value)
                {
                    this.errorProvider1.SetError(this.dateEnd, "invalid");
                    return;
                }
                else
                {
                    this.errorProvider1.SetError(this.dateEnd, string.Empty);
                }
                searchwhere = searchwhere + string.Format("  and plan_date between '{0}'   and '{1}'"
                    , this.dateStart.Value.ToShortDateString()
                    , this.dateEnd.Value.AddDays(1).ToShortDateString()
                    );
            }

            if(cbFormulas.SelectedIndex>0)
            {
                searchwhere = searchwhere + "    and  formula_id="+(cbFormulas.SelectedItem as FormulaInfo).FormulaId;
            }

            string orwhere = "1<>1";

            if (this.cbDraft.Checked)
            {
                orwhere = orwhere + "   or  plan_status= "+(int)PlanStatus.draft;
            }

            if (this.cbPending.Checked)
            {
                orwhere = orwhere + "   or  plan_status= " + (int)PlanStatus.pending;
            }

            if (this.cbdone.Checked)
            {
                orwhere = orwhere + "   or  plan_status= " + (int)PlanStatus.done;
            }

            if (this.cbDraft.Checked || this.cbPending.Checked || this.cbdone.Checked)
            {
                searchwhere = searchwhere + string.Format("   and   ({0})", orwhere);
            }


            if (SearchCompleted != null)
            {
                this.SearchCompleted(searchwhere);
            }
            this.Close();

        }

        public event SeachCompletedhanlder SearchCompleted;
    }
}
